package com.godpaper.the3directs.busniess.AI
{
	import com.godpaper.the3directs.busniess.managers.PlayerManager;
	import com.godpaper.the3directs.model.BoardModel;
	import com.godpaper.the3directs.model.PiecesModel;
	import com.godpaper.the3directs.util.NumberUtil;
	import com.godpaper.the3directs.util.PositionTransactUtil;
	import com.godpaper.the3directs.views.components.GasketButton;
	import com.godpaper.the3directs.vo.ConductVO;
	
	import de.polygonal.ds.Array2;
	/**
	 * 
	 * @author Knight-errant(假进攻)
	 * 
	 */	
	public class FalseAttactAI extends GameAIBase implements IAIBehavior
	{
		/**
		 * 
		 * @param gamePosition
		 * 
		 */		
		public function FalseAttactAI(gamePosition:Array2)
		{
			super(gamePosition);
			bestMove = new ConductVO();
			moves =  generateMoves(PiecesModel.getInstance().redPiecesCollection,gamePosition);
			if(moves.length<=0)
			{
				PlayerManager.humanWin();//pluge to death.
			}else
			{
				trace("all possbility moves:",moves.toArray().toString());
				var randomStep:int = NumberUtil.randomNumberWithScope(0,moves.length-1);
				trace("randomStep:",randomStep);
				var pValue:int=-1;
				for(var i:int=0;i<moves.length;i++)
				{
					if(doEvaluation(moves.getItemAt(i) as ConductVO)>pValue)
					{
						bestMove = (moves.getItemAt(i) as ConductVO);
						pValue = doEvaluation(moves.getItemAt(i) as ConductVO);
					}
				}
//				bestMove = moves.getItemAt(randomStep) as ConductVO;
				trace("randomed bestMove:",bestMove);
				trace("max position value:",pValue);
				applyMovement(bestMove);
			}
		}
		
		override public function doEvaluation(conductVO:ConductVO):int
		{
			var leftValue:int;
			var topValue:int;
			var rightValue:int;
			var bottomValue:int;
			//开局大三角中间进攻位置最佳,优先选择!
			var leftValuePlus:int;//靠左附加值
			var topValuePlus:int;//靠上附加值
			var rightValuePlus:int;//靠右附加值
			var bottomValuePlus:int;//靠下附加值
			
			var currentGB:GasketButton = BoardModel.getInstance().gasketsPosition.gett( conductVO.newPosition[0],
																						conductVO.newPosition[1]) as GasketButton;
			if(currentGB==null)
			{
				return 0;
			}
			//left
			if(currentGB.left)
			{
				if( (currentGB.left.styleName == GasketButton.STYLE_TRIANGLE_RED)
					|| (currentGB.left.styleName == GasketButton.STYLE_TRIANGLE_RED_R180))
				{
					leftValue++;
				}	
				if( 28<=PositionTransactUtil.transactArray2Index(currentGB.left.position)
				   && 54>=PositionTransactUtil.transactArray2Index(currentGB.left.position) )
				{
					leftValuePlus++;
				}   
			}
			//top
			if(currentGB.top)
			{
				if( (currentGB.top.styleName == GasketButton.STYLE_TRIANGLE_RED)
					|| (currentGB.top.styleName == GasketButton.STYLE_TRIANGLE_RED_R180))
				{
					topValue++;
				}	
				if( 28<=PositionTransactUtil.transactArray2Index(currentGB.top.position)
				   && 54>=PositionTransactUtil.transactArray2Index(currentGB.top.position) )
				{
					topValuePlus++;
				}  
			}
			//right
			if(currentGB.right)
			{
				if( (currentGB.right.styleName == GasketButton.STYLE_TRIANGLE_RED)
					|| (currentGB.right.styleName == GasketButton.STYLE_TRIANGLE_RED_R180))
				{
					rightValue++;
				}	
				if( 28<=PositionTransactUtil.transactArray2Index(currentGB.right.position)
				   && 54>=PositionTransactUtil.transactArray2Index(currentGB.right.position) )
				{
					rightValuePlus++;
				}  
			}
			//bottom
			if(currentGB.bottom)
			{
				if( (currentGB.bottom.styleName == GasketButton.STYLE_TRIANGLE_RED)
					|| (currentGB.bottom.styleName == GasketButton.STYLE_TRIANGLE_RED_R180))
				{
					bottomValuePlus++;
				}	
				if( 28<=PositionTransactUtil.transactArray2Index(currentGB.bottom.position)
				   && 54>=PositionTransactUtil.transactArray2Index(currentGB.bottom.position) )
				{
					leftValuePlus++;
				}  
			}
			trace("doEvaluation's returned value sum :",(leftValue+topValue+rightValue+bottomValue+leftValuePlus+topValuePlus+rightValuePlus+bottomValuePlus));
			return (leftValue+topValue+rightValue+bottomValue+leftValuePlus+topValuePlus+rightValuePlus+bottomValuePlus);
		}
	}
}